<?php


namespace app\service;


use app\entity\SysMenuEntity;
use app\entity\SysUserEntity;
use app\exception\ErrorCode;
use app\exception\OaException;
use app\models\request\ReqMenu;
use app\models\response\RespMenuDetail;

class SysMenuService
{
    public function selectMenuListByUserIdSql($userId) {

        $where = "";
        if ($userId !=1) {
            $where = " and ur.user_id=".$userId;
        }
        $sql = "SELECT DISTINCT
            m.menu_id,
            m.parent_id,
            m.`name`,
            m.url,
            ifnull(m.perms, '') AS perms,
            m.type,
            m.icon,
            m.order_num
        FROM
            sys_menu m
        LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
        LEFT JOIN sys_user_role ur ON rm.role_id = ur.role_id
        LEFT JOIN sys_role ro ON ur.role_id = ro.role_id
        WHERE 
            type!=2 ".$where."
        ORDER BY
            m.parent_id,
            m.order_num";

        return SysUserEntity::getDb()->createCommand($sql)->queryAll();
    }



    public function selectMenuListByUserId($userId)
    {
        $service = new SysUserRoleService();
        $arr = $service->queryRoleIdList($userId);


        foreach ($arr as $e) {
            if ($e->getRoleId() == 1) {
                $userId = 1;
                break;
            }
        }
        $list = $this->selectMenuListByUserIdSql($userId);
        if (!$list) return null;
        $arr=[];

        foreach ($list as $detail) {
            $resp = new RespMenuDetail();
            $resp->setMenuId($detail["menu_id"]);
            $resp->setParentId($detail["parent_id"]);
            $resp->setName($detail["name"]);
            $resp->setUrl($detail["url"]);
            $resp->setPerms($detail["perms"]);
            $resp->setType($detail["type"]);
            $resp->setIcon($detail["icon"]);
            $resp->setOrderNum($detail["order_num"]);
            $arr[] = $resp;
        }
        return $arr;
    }

    /**
     * @return array|null
     */
    public function menuList() {
        $list = SysMenuEntity::find()->all();
        $arr=[];
        if (!$list) return $arr;
        foreach ($list as $detail) {
            $resp = new RespMenuDetail();
            $resp->setMenuId($detail->menu_id);
            $resp->setParentId($detail->parent_id);
            $resp->setName($detail->name);
            $resp->setUrl($detail->url);
            $resp->setPerms($detail->perms);
            $resp->setType($detail->type);
            $resp->setIcon($detail->icon);
            $resp->setOrderNum($detail->order_num);
            $arr[] = $resp;
        }
        return $arr;
    }

    public function update(ReqMenu $req) {
        if ($req->getMenuId()) {
            $entity = SysMenuEntity::find()->where(["menu_id"=>$req->getMenuId()])->one();
        } else {
            $entity = new SysMenuEntity();
        }



        $entity->setParentId($req->getParentId());
        $entity->setName($req->getName());
        $entity->setUrl($req->getUrl());
        $entity->setPerms($req->getPerms());
        $entity->setType($req->getType());
        $entity->setIcon($req->getIcon());
        $entity->setOrderNum($req->getOrderNum());
        $entity->save();
        return true;
    }

    public function deleteBatch($idList) {
        $rows = SysMenuEntity::find()->where(['in', 'parent_id', $idList])->all();
        if ($rows  && sizeof($rows) >0 ) {
            throw new OaException(ErrorCode::EXISTS_SUB_MENU);
        }
        SysMenuEntity::deleteAll(['in', 'menu_id', $idList]);
        return true;
    }
}